#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QTimer>
#include <qdebug.h>
#include <QMouseEvent>

unsigned int style_flag = 0;

bool counter_switch_flag = false;
QTimer *timer;
QTimer *rest_timer;
unsigned int timer_count = 0;
unsigned int timer_total;

unsigned int rest_count = 0;
unsigned int rest_total;

bool window_move_flag = true;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{

    ui->setupUi(this);
    ui->lineEdit->setValidator(new QIntValidator(0,999999,this));

    ui->pushButton_skin->setIcon(QIcon(":/skin_black.svg"));
    ui->pushButton->setIcon(QIcon(":/close_black.svg"));
    ui->pushButton_2->setIcon(QIcon(":/back_black.svg"));
    ui->pushButton_skin->setIconSize(QSize(30,30));
    ui->pushButton->setIconSize(QSize(30,30));

    timer = new QTimer(this);
    rest_timer = new QTimer(this);
    rest_timer->setInterval(1000);
    rest_timer->stop();
    connect(timer, SIGNAL(timeout()), this, SLOT(update()));
    connect(rest_timer, SIGNAL(timeout()), this, SLOT(rest_update()));

    setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
    setAttribute(Qt::WA_TranslucentBackground);

}

MainWindow::~MainWindow()
{
    delete ui;
}



//窗口可拖动功能实现
void MainWindow::mousePressEvent(QMouseEvent *e)
{
    if(window_move_flag == true)
    {
        last = e->globalPos();
    }
}

void MainWindow::mouseMoveEvent(QMouseEvent *e)
{
    if(window_move_flag == true)
    {
        int dx = e->globalX() - last.x();
        int dy = e->globalY() - last.y();

        move(x()+dx, y()+dy);
        last = e->globalPos();
    }
}
void MainWindow::mouseReleaseEvent(QMouseEvent *e)
{
    if(window_move_flag == true)
    {
        int dx = e->globalX() - last.x();
        int dy = e->globalY() - last.y();
        move(x()+dx, y()+dy);
        last = e->globalPos();
    }
}

void MainWindow::on_pushButton_reset_clicked()
{
    timer_count = 0;
    timer_total = 99999;
    rest_count = 0;
    rest_total = 5;

    ui->lineEdit->setText("30");
    ui->lineEdit_rest->setText("5");
    timer->stop();
}

void MainWindow::on_pushButton_start_clicked()
{
    counter_switch_flag = true;
    bool ok;
    timer_total = ui->lineEdit->text().toInt(&ok,10)*60;
//    timer_total = ui->lineEdit->text().toInt(&ok,10)*5;

    timer->start(1000);
//    ui->pushButton_start->setText("...");
    QMainWindow::hide();
    window_move_flag = false;
}

void MainWindow::update()
{
    QString time_format = 0;
    if(timer_count < timer_total)
    {
        timer_count++;
        time_format.append(QString::number((timer_total - timer_count)/60,10));
        time_format.append(":");
        time_format.append(QString::number((timer_total - timer_count)%60,10));
//        ui->pushButton_start->setText(time_format);
        qDebug() << "时间剩余 "+ time_format;

    }
    else
    {
        timer->stop();

        rest_timer->start();
    }
}

void MainWindow::rest_update()
{
    bool ok;
    rest_total = ui->lineEdit_rest->text().toInt(&ok,10)*60;;

    ui->stackedWidget->setCurrentIndex(1);
//    QMainWindow::setWindowOpacity(0.75);
    QMainWindow::showFullScreen();

//    QMainWindow::setStyleSheet("border-radius:0px");

    if(rest_count < rest_total)
    {
        QString rest_format = "看屏幕时间超过了 分钟，建议休息一会\n";

        rest_format.insert(9,QString::number(timer_total/60,10));

        rest_format.append("                     休息剩余：");
        rest_format.append(QString::number((rest_total - rest_count)/60,10));
        rest_format.append(" : ");
        rest_format.append(QString::number((rest_total - rest_count)%60,10));

        ui->label_timeup->setText(rest_format);

        rest_count++;

        qDebug() << "休息剩余 "+ QString::number((rest_total - rest_count)%60,10);
    }
    else
    {
        rest_timer->stop();

        timer_count = 0;
        timer_total = 99999;
        rest_count = 0;
        rest_total = 5;

        window_move_flag = true;

        ui->stackedWidget->setCurrentIndex(0);
        QMainWindow::showNormal();
//        QMainWindow::setWindowOpacity(0.98);

    }
}


void MainWindow::on_pushButton_clicked()
{
    QMainWindow::close();
}

void MainWindow::on_pushButton_2_clicked()
{
    rest_timer->stop();

    timer_count = 0;
    timer_total = 99999;
    rest_total = 5;
    rest_count = 0;

    window_move_flag = true;
    ui->stackedWidget->setCurrentIndex(0);
//    QMainWindow::setWindowOpacity(0.98);
    QMainWindow::showNormal();

}


void MainWindow::on_pushButton_skin_clicked()
{
    style_check();
}

void MainWindow::style_check()
{

    if(style_flag == 0)
    {
        style_flag = 1;

        ui->pushButton_skin->setIcon(QIcon(":/skin_white.svg"));
        ui->pushButton->setIcon(QIcon(":/close_white.svg"));
        ui->pushButton_2->setIcon(QIcon(":/back_white.svg"));

        QFile file(":/dark.qss");
        file.open(QFile::ReadOnly | QFile::Text);
        QTextStream stream(&file);
        this->setStyleSheet(stream.readAll());

    }
    else
    {
        style_flag = 0;

        ui->pushButton_skin->setIcon(QIcon(":/skin_black.svg"));
        ui->pushButton->setIcon(QIcon(":/close_black.svg"));
        ui->pushButton_2->setIcon(QIcon(":/back_black.svg"));

        QFile file1(":/light.qss");
        file1.open(QFile::ReadOnly | QFile::Text);
        QTextStream stream1(&file1);
        this->setStyleSheet(stream1.readAll());
    }
}
